﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Twittit.WPF
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DataAccess.User current;

        public MainWindow(Twittit.DataAccess.User i)
        {
            InitializeComponent();
            this.Name = "Main";
            MainViewModel viewModel = this.DataContext as MainViewModel;
            viewModel.init(i);
            current = viewModel.User;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            foreach (System.Windows.Window w in App.Current.Windows)
            {
                w.Close();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            refreshSearch();
        }

        private void listUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Twittit.DataAccess.User user = ((sender as ListBox).SelectedItem as DataAccess.User);
            if (user != null)
            {
                if (Business.UserService.getInstance().hasSubscribed(current.id, user.id))
                {
                    button1.Visibility = System.Windows.Visibility.Hidden;
                    button2.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    button1.Visibility = System.Windows.Visibility.Visible;
                    button2.Visibility = System.Windows.Visibility.Hidden;
                }

                avatar.Source = ((ImageSource)(System.ComponentModel.TypeDescriptor.GetConverter(typeof(ImageSource)).ConvertFromInvariantString(@user.getGravatarURL())));
                nbTwittitsUser.Content = Business.UserService.getInstance().countUserPosts(user.id);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Business.UserService.getInstance().subscribe(current.id,((DataAccess.User)listUser.SelectedItem).id);
            button1.Visibility = System.Windows.Visibility.Hidden;
            button2.Visibility = System.Windows.Visibility.Visible;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Business.UserService.getInstance().unSubscribe(current.id, ((DataAccess.User)listUser.SelectedItem).id);
            button1.Visibility = System.Windows.Visibility.Visible;
            button2.Visibility = System.Windows.Visibility.Hidden;
        }

        private void listRequests_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Twittit.DataAccess.User user = ((sender as ListBox).SelectedItem as DataAccess.User);
            if (user != null)
            {
                button3.Visibility = System.Windows.Visibility.Visible;
                button4.Visibility = System.Windows.Visibility.Visible;
                image2.Source = ((ImageSource)(System.ComponentModel.TypeDescriptor.GetConverter(typeof(ImageSource)).ConvertFromInvariantString(@user.getGravatarURL())));
            }
        }

        private void tabItem1_Loaded(object sender, RoutedEventArgs e)
        {
            List<Twittit.DataAccess.User> users = Twittit.Business.UserService.getInstance().getRequests(current.id);
            listBoxRequests.Items.Clear();
            foreach (Twittit.DataAccess.User u in users)
                listBoxRequests.Items.Add(u);

            button3.Visibility = System.Windows.Visibility.Hidden;
            button4.Visibility = System.Windows.Visibility.Hidden;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Business.UserService.getInstance().acceptRequest(((DataAccess.User)listBoxRequests.SelectedItem).id, current.id);
            listBoxRequests.Items.Remove(listBoxRequests.SelectedItem);
            listBoxRequests.Items.Refresh();

            button3.Visibility = System.Windows.Visibility.Hidden;
            button4.Visibility = System.Windows.Visibility.Hidden;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Business.UserService.getInstance().denyRequest(((DataAccess.User)listBoxRequests.SelectedItem).id, current.id);
            listBoxRequests.Items.Remove(listBoxRequests.SelectedItem);
            listBoxRequests.Items.Refresh();

            button3.Visibility = System.Windows.Visibility.Hidden;
            button4.Visibility = System.Windows.Visibility.Hidden;
        }

        private void refreshSearch()
        {
            List<Twittit.DataAccess.User> lu = Twittit.Business.UserService.getInstance().searchUser(textRecherche.Text);
            listUser.Items.Clear();
            foreach (Twittit.DataAccess.User u in lu)
                listUser.Items.Add(u);
        }

        private void textRecherche_TextChanged(object sender, TextChangedEventArgs e)
        {
            refreshSearch();
        }

        private void TabItemRecherche_Loaded(object sender, RoutedEventArgs e)
        {
            refreshSearch();
        }

        private void toggleButton_Click(object sender, RoutedEventArgs e)
        {
            tabControl.SelectedItem = tabItem1;
        }
    }
}
